export {}
declare global {

    export interface Map<K,V> {
        // toJSON():string;
    }
    export interface Array<T> {
        mapPromise<U>(callbackfn: (value: T, index: number, array: T[]) => Promise<U>): Promise<U[]>;
        mapStringPromise(callbackfn: (value: T, index: number) => Promise<any>): Promise<string>;
    }
}

// Map.prototype.toJSON = function () {
//     var ret = "{";
//     this.forEach((v, k)=> {
//         ret += '"' + k + '":' + JSON.stringify(v) + ",";
//     })
//     ret = ret.removeLast() + "}";
//     return ret;
// }

Object.defineProperty(Array.prototype, "mapPromise", {
    value: async function (callbackfn: (value: any, index: number, array: any[]) => Promise<any>) {
        var arr = new Array<any>();
        for (var i = 0; i < this.length; i++) {
            arr.push(await callbackfn(this[i], i, arr));
        }
        return arr;
    },
    enumerable: false,
});


Object.defineProperty(Array.prototype, "mapStringPromise", {
    value: async function (callbackfn: (value: any, index: number) => Promise<any>) {
        var str = ""
        for (var i = 0; i < this.length; i++) {
            var ret = await callbackfn(this[i], i);
            if (ret != null)
                str += ret;
        }
        return str;
    },
    enumerable: false,
});